//
//  LzwBroadcastVC.m
//  CoJellyCartoon
//
//  Created by 刘志武 on 16/4/5.
//  Copyright © 2016年 CJG. All rights reserved.
//

#import "LzwBroadcastVC.h"
#import "LzwBroadcastCell.h"
#import "LzwBangumiDetailVC.h"

@interface LzwBroadcastVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *myBroadTableView;


@end

@implementation LzwBroadcastVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"放送";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 20, 20);
    [button setImage:[[UIImage imageNamed:@"L20.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(LeftButtonAxtion:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = barItem;
    
    
    //初始化数组
    self.arrayAll = [NSMutableArray array];
    
    [self creatBroadTableView];
    
}
- (void)creatBroadTableView{
    
    self.myBroadTableView = ({
    
        UITableView *tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.delegate = self;
        tableView.dataSource = self;
    
        [self.view addSubview:tableView];
        tableView;
    
    });
    
    //注册cell;
    [self.myBroadTableView registerClass:[LzwBroadcastCell class] forCellReuseIdentifier:@"cell"];
    
    [GiFHUD setGifWithImageName:@"hold.gif"];
    [GiFHUD show];
    
    
    //请求数据
    [self getBroadcastData];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrayAll.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LzwBroadcastCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.modelB = _arrayAll[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 150;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    

    LzwBangumiDetailVC *bangVc = [[LzwBangumiDetailVC alloc]init];
    
    LzwBroadcastModle *modelC = _arrayAll[indexPath.row];
    
    bangVc.ting_ID = modelC.season_id.intValue;
    
    [self.navigationController pushViewController:bangVc animated:YES];
    
}

- (void)getBroadcastData{
    
    NSString *str = @"http://app.bilibili.com/bangumi/timeline_v2?_device=android&_hwid=e647d7e136a2c2a9&_ulv=10000&access_key=ac2771d488a63b7c3f8ccf926d8fe761&appkey=c1b107428d337928&build=414000&platform=android&ts=1459404390000&sign=7390ec3e70d5463b8e9ee2061e642831";
    
    [WZAFNetTool GetNetWithURL:str WithBody:nil headfile:nil response:WZJSON success:^(id reuslt) {
        
        NSMutableArray *array = [reuslt objectForKey:@"list"];
        
        for (NSMutableDictionary *dict in array) {
            
            LzwBroadcastModle *modle = [[LzwBroadcastModle alloc]init];
            
            [modle setValuesForKeysWithDictionary:dict];
            
            [self.arrayAll addObject:modle];
            
            
        }

        [self.myBroadTableView reloadData];
        [GiFHUD dismiss];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
    
}


- (void)LeftButtonAxtion:(UIButton *)btn{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


@end
